package com.woniuxy.controllers;

import com.github.pagehelper.PageInfo;
import com.woniuxy.entitys.Lost;
import com.woniuxy.entitys.RestPOJO;
import com.woniuxy.services.LostService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "客户流失管理接口")
public class LostController {
    @Autowired
    private LostService lostService;

    @ApiOperation(
            value = "按年份获取客户流失数量",
            httpMethod = "GET",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class
    )
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name="year",value="查询年份",required=true),
    })
    //客户流失折线图（按年份查询）
    @GetMapping("/lost/count")
    public RestPOJO getLostCount(Integer year){
        RestPOJO restPOJO = new RestPOJO();
        List<Map<String , Integer>> list = lostService.getLostCount(year);
        if (list != null ) {
            restPOJO.setMessage("查询数据成功");
            restPOJO.setStatus(1000);
            restPOJO.setData(list);

        } else {
            restPOJO.setData(list);
            restPOJO.setMessage("查询数据失败");
            restPOJO.setStatus(1001);
        }
        return restPOJO;
    }

    @ApiOperation(
            value = "按照客户经理id或公司id查询流失记录",
            httpMethod = "GET",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class
    )
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name="user_id",value="客户经理id",required=false),
            @ApiImplicitParam(paramType="query",name="user_id",value="公司id",required=false),
    })
    //按照客户经理或公司名称查询流失记录
    @GetMapping("/lost")
    public RestPOJO getLostByCidOrUid(Integer user_id,Integer com_id){
        RestPOJO restPOJO = new RestPOJO();
        List<Lost> list = lostService.getLostByCidOrUid(user_id,com_id);
        if (list != null ) {
            restPOJO.setMessage("查询数据成功");
            restPOJO.setStatus(1000);
            restPOJO.setData(list);

        } else {
            restPOJO.setData(list);
            restPOJO.setMessage("查询数据失败");
            restPOJO.setStatus(1001);
        }
        return restPOJO;
    }

    @GetMapping("/selectAllLost")
    @ApiOperation(
            value = "查询所有六个月没下单的客户",
            httpMethod = "GET",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class
    )
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name="pagenow1",value="当前页",required=true),
            @ApiImplicitParam(paramType="query",name="pagesize",value="每页显示的条目数",required=true)

    })
    public RestPOJO selectAllLost(Integer pagenow1,Integer pagesize) {
        RestPOJO restPOJO = new RestPOJO();

        PageInfo<Lost> pageInfo = lostService.selectAll(pagenow1, pagesize);
        List<Lost> lost1 =  pageInfo.getList();
        for (Lost a:lost1) {
            if (a.getLost_status() == 5) {
                a.setLost_status(0);
                lostService.updateByPrimaryKeySelective(a);
            }
        }
//            if (a.getLost_status()!=null){
//                a.setLost_status(2);
//                lostService.updateByPrimaryKeySelective(a);
//            }

        if (pageInfo != null && pageInfo.getList().size() > 0) {
            restPOJO.setMessage("查询数据成功");
            restPOJO.setStatus(200);
            restPOJO.setData(pageInfo);

        } else {
            restPOJO.setData(pageInfo);
            restPOJO.setMessage("查询数据失败");
            restPOJO.setStatus(10001);
        }
        return restPOJO;

    }


    @GetMapping("/selectByCnameOrStatus")
    @ApiOperation(
            value = "根据状态/公司名字/客户名字查询",
            httpMethod = "GET",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class
    )
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name="pagenow1",value="当前页",required=true),
            @ApiImplicitParam(paramType="query",name="pagesize",value="每页显示的条目数",required=true),
            @ApiImplicitParam(paramType="query",name="lost_status",value="流失状态",required=true),
            @ApiImplicitParam(paramType="query",name="com_name",value="公司名字",required=true),
            @ApiImplicitParam(paramType="query",name="user_name",value="客户经理名字",required=true)


    })
    public RestPOJO selectByCnameOrStatus(Integer pagenow1, Integer pagesize, Integer lost_status, String com_name, String user_name) {
        RestPOJO restPOJO = new RestPOJO();
        PageInfo<Lost> pageInfo = lostService.selectByCnameOrStatus(pagenow1, pagesize,lost_status,com_name,user_name);


        if (pageInfo != null && pageInfo.getList().size() > 0) {
            restPOJO.setMessage("查询数据成功");
            restPOJO.setStatus(200);
            restPOJO.setData(pageInfo);

        } else {
            restPOJO.setData(pageInfo);
            restPOJO.setMessage("查询数据失败");
            restPOJO.setStatus(10001);
        }
        return restPOJO;

    }

  //000


    @GetMapping("/selectByid/{lost_id}")
    @ApiOperation(
            value = "根据id查询",
            httpMethod = "GET",
            protocols = "HTTP",
            produces = "application/json",
            response = RestPOJO.class
    )
    @ApiImplicitParams({

            @ApiImplicitParam(paramType="path",name="user_id",value="客户id",required=true)


    })
    public RestPOJO selectByid(@PathVariable("lost_id") Integer lost_id) {
        RestPOJO restPOJO = new RestPOJO();
        Lost lost=lostService.selectById(lost_id);

        System.out.println(lost);
        if (lost != null) {
            restPOJO.setMessage("查询数据成功");
            restPOJO.setStatus(200);
            restPOJO.setData(lost);

        } else {
            restPOJO.setMessage("查询数据失败");
            restPOJO.setStatus(10001);
        }
        return restPOJO;

    }






  @PostMapping("/updateByPrimaryKeySelective")
  @ApiOperation(
          value = "根据id修改lost实体类（id为必填项，其它可以选填）",
          httpMethod = "POST",
          protocols = "HTTP",
          produces = "application/json",
          response = RestPOJO.class
  )
//  @ApiImplicitParams({
//          @ApiImplicitParam(paramType="body",name="lost",value="客户流失表",required=true)
//  })
  //如果传过来实体类，则不需要写上边的
    public RestPOJO updateByPrimaryKeySelective(@RequestBody Lost lost) {
        RestPOJO restPOJO = new RestPOJO();
        Integer ii=lostService.updateByPrimaryKeySelective(lost);
        if (ii > 0) {
            restPOJO.setMessage("修改数据成功");
            restPOJO.setStatus(200);

        } else {
            restPOJO.setMessage("修改数据失败");
            restPOJO.setStatus(10001);
        }
        return restPOJO;

    }







}




